﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Request.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Text;

    #endregion

    /// <summary>
    ///     The request.
    /// </summary>
    public sealed class Request
    {
        #region Fields

        /// <summary>
        ///     The questions.
        /// </summary>
        private readonly List<Question> questions;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="Request" /> class.
        /// </summary>
        public Request()
        {
            this.Header = new Header { OpCodeType = OPCodeType.Query, QdCount = 0 };

            this.questions = new List<Question>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the data.
        /// </summary>
        /// <value>The data.</value>
        public IEnumerable<byte> Data
        {
            get
            {
                var data = new List<byte>();
                this.Header.QdCount = (ushort)this.questions.Count;
                data.AddRange(this.Header.Data);
                foreach (Question question in this.questions)
                {
                    data.AddRange(question.Data);
                }

                return data.ToArray();
            }
        }

        /// <summary>
        ///     Gets or sets the header.
        /// </summary>
        /// <value>The header.</value>
        public Header Header { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Adds the question.
        /// </summary>
        /// <param name="question">
        /// The question.
        /// </param>
        public void AddQuestion(Question question)
        {
            if (question == null)
            {
                throw new ArgumentNullException("question");
            }

            this.questions.Add(question);
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(" Header: {0}, OpCodeType: {1}, RecursionDesired: {2} : ", this.Header.Id, this.Header.OpCodeType, this.Header.RecursionDesired);

            int questionId = 0;

            if (this.questions != null)
            {
                foreach (var question in this.questions)
                {
                    sb.AppendFormat("Question #{0} : {1}, ", questionId, question);
                    questionId++;    
                }
            }

            return sb.ToString();
        }

        #endregion
    }
}